//
//  SearchView.swift
//  YueChat
//
//  Created by IMac on 2024/9/18.
//

import UIKit

protocol ContactSearchDelegate :AnyObject {
    
    func blockSearchStr (Str:String)
}

enum SearchType {
    case contact
    case share
}

class SearchView: UIView ,UITextFieldDelegate{

    @IBOutlet weak var tf: UITextField!
    @IBOutlet weak var bgView: UIView!
//    var blockSearchStr: ((String) -> ())?
    weak var delegate:ContactSearchDelegate?
    var searchType: SearchType!
    
//    required init?(coder: NSCoder) {
//        super.init(coder: coder)
//        self.bgView.layer.cornerRadius = 5
//    }
    
    func loadFromNib() -> UIView {
//        self.blockSearchStr = blockSearchStr
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: "SearchView", bundle: bundle)
        return nib.instantiate(withOwner: self, options: nil).first as! UIView
    }


    
//    init(blockSearchStr: ( (String) -> Void)? = nil) {
//        super.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 160))
//        self.blockSearchStr = blockSearchStr
//    }
////    
////
////    
////    
//    required init?(coder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        if searchType == .contact {
//            self.blockSearchStr!("")
            self.delegate?.blockSearchStr(Str: "")
        }
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        if searchType == .share {
//            self.blockSearchStr!(textField.text!)
            self.delegate?.blockSearchStr(Str: textField.text!)
        }
        
        return true
    }
    
    
}
